/*============================================================================
   Copyright (c) 2011-2012 Qualcomm Technologies, Inc.  All Rights Reserved.
   Qualcomm Technologies Proprietary and Confidential.
============================================================================*/
#include "camera_dbg.h"
#include "vfe_util_common.h"
#include "vfe.h"
#include "gamma.h"
#include "vfe_tgtcommon.h"

#ifdef ENABLE_GAMMA_LOGGING
  #undef CDBG
  #define CDBG LOGE
#endif

#define DEFAULT_CONTRAST 5

static const uint8_t ipl_gammaSigmoid0_5[256] = {
  0,  11,  16,  20,  23,  25,  28,  30,  32,  34,
 36,  37,  39,  41,  42,  44,  45,  47,  48,  49,
 50,  52,  53,  54,  55,  56,  58,  59,  60,  61,
 62,  63,  64,  65,  66,  67,  68,  69,  70,  71,
 71,  72,  73,  74,  75,  76,  77,  77,  78,  79,
 80,  81,  81,  82,  83,  84,  84,  85,  86,  87,
 87,  88,  89,  90,  90,  91,  92,  92,  93,  94,
 94,  95,  96,  96,  97,  98,  98,  99, 100, 100,
101, 102, 102, 103, 103, 104, 105, 105, 106, 107,
107, 108, 108, 109, 109, 110, 111, 111, 112, 112,
113, 113, 114, 115, 115, 116, 116, 117, 117, 118,
118, 119, 119, 120, 121, 121, 122, 122, 123, 123,
124, 124, 125, 125, 126, 126, 127, 127, 128, 128,
129, 129, 130, 130, 131, 131, 132, 132, 133, 133,
134, 134, 135, 136, 136, 137, 137, 138, 138, 139,
139, 140, 140, 141, 142, 142, 143, 143, 144, 144,
145, 146, 146, 147, 147, 148, 148, 149, 150, 150,
151, 152, 152, 153, 153, 154, 155, 155, 156, 157,
157, 158, 159, 159, 160, 161, 161, 162, 163, 163,
164, 165, 165, 166, 167, 168, 168, 169, 170, 171,
171, 172, 173, 174, 174, 175, 176, 177, 178, 178,
179, 180, 181, 182, 183, 184, 184, 185, 186, 187,
188, 189, 190, 191, 192, 193, 194, 195, 196, 197,
199, 200, 201, 202, 203, 205, 206, 207, 208, 210,
211, 213, 214, 216, 218, 219, 221, 223, 225, 227,
230, 232, 235, 239, 244, 255
};

static const uint8_t ipl_gammaSigmoid0_6[256] = {
  0,   7,  11,  13,  16,  18,  20,  22,  24,  26,
 28,  29,  31,  32,  34,  35,  37,  38,  39,  41,
 42,  43,  44,  46,  47,  48,  49,  50,  51,  52,
 54,  55,  56,  57,  58,  59,  60,  61,  62,  63,
 64,  65,  65,  66,  67,  68,  69,  70,  71,  72,
 73,  74,  74,  75,  76,  77,  78,  79,  79,  80,
 81,  82,  83,  84,  84,  85,  86,  87,  87,  88,
 89,  90,  90,  91,  92,  93,  93,  94,  95,  96,
 96,  97,  98,  99,  99, 100, 101, 101, 102, 103,
103, 104, 105, 106, 106, 107, 108, 108, 109, 110,
110, 111, 112, 112, 113, 113, 114, 115, 115, 116,
117, 117, 118, 119, 119, 120, 120, 121, 122, 122,
123, 124, 124, 125, 125, 126, 127, 127, 128, 128,
129, 130, 130, 131, 131, 132, 133, 133, 134, 135,
135, 136, 136, 137, 138, 138, 139, 140, 140, 141,
142, 142, 143, 143, 144, 145, 145, 146, 147, 147,
148, 149, 149, 150, 151, 152, 152, 153, 154, 154,
155, 156, 156, 157, 158, 159, 159, 160, 161, 162,
162, 163, 164, 165, 165, 166, 167, 168, 168, 169,
170, 171, 171, 172, 173, 174, 175, 176, 176, 177,
178, 179, 180, 181, 181, 182, 183, 184, 185, 186,
187, 188, 189, 190, 190, 191, 192, 193, 194, 195,
196, 197, 198, 199, 200, 201, 203, 204, 205, 206,
207, 208, 209, 211, 212, 213, 214, 216, 217, 218,
220, 221, 223, 224, 226, 227, 229, 231, 233, 235,
237, 239, 242, 244, 248, 255
};

static const uint8_t ipl_gammaSigmoid0_7[256] = {
  0,   4,   7,   9,  11,  13,  15,  17,  18,  20,
 21,  23,  24,  26,  27,  29,  30,  31,  32,  34,
 35,  36,  37,  38,  40,  41,  42,  43,  44,  45,
 46,  47,  48,  50,  51,  52,  53,  54,  55,  56,
 57,  58,  59,  60,  61,  62,  62,  63,  64,  65,
 66,  67,  68,  69,  70,  71,  72,  73,  73,  74,
 75,  76,  77,  78,  79,  80,  80,  81,  82,  83,
 84,  85,  85,  86,  87,  88,  89,  90,  90,  91,
 92,  93,  94,  94,  95,  96,  97,  98,  98,  99,
100, 101, 101, 102, 103, 104, 105, 105, 106, 107,
108, 108, 109, 110, 111, 111, 112, 113, 114, 114,
115, 116, 116, 117, 118, 119, 119, 120, 121, 121,
122, 123, 124, 124, 125, 126, 126, 127, 128, 129,
129, 130, 131, 131, 132, 133, 134, 134, 135, 136,
136, 137, 138, 139, 139, 140, 141, 141, 142, 143,
144, 144, 145, 146, 147, 147, 148, 149, 150, 150,
151, 152, 153, 154, 154, 155, 156, 157, 157, 158,
159, 160, 161, 161, 162, 163, 164, 165, 165, 166,
167, 168, 169, 170, 170, 171, 172, 173, 174, 175,
175, 176, 177, 178, 179, 180, 181, 182, 182, 183,
184, 185, 186, 187, 188, 189, 190, 191, 192, 193,
193, 194, 195, 196, 197, 198, 199, 200, 201, 202,
203, 204, 205, 207, 208, 209, 210, 211, 212, 213,
214, 215, 217, 218, 219, 220, 221, 223, 224, 225,
226, 228, 229, 231, 232, 234, 235, 237, 238, 240,
242, 244, 246, 248, 251, 255
};

static const uint8_t ipl_gammaSigmoid0_8[256] = {
  0,   3,   5,   6,   8,  10,  11,  13,  14,  15,
 17,  18,  19,  21,  22,  23,  24,  25,  27,  28,
 29,  30,  31,  32,  34,  35,  36,  37,  38,  39,
 40,  41,  42,  43,  44,  45,  46,  47,  48,  49,
 50,  51,  52,  53,  54,  55,  56,  57,  58,  59,
 60,  61,  62,  63,  64,  65,  66,  67,  68,  69,
 70,  71,  72,  73,  73,  74,  75,  76,  77,  78,
 79,  80,  81,  82,  83,  83,  84,  85,  86,  87,
 88,  89,  90,  90,  91,  92,  93,  94,  95,  96,
 96,  97,  98,  99, 100, 101, 102, 102, 103, 104,
105, 106, 107, 107, 108, 109, 110, 111, 112, 112,
113, 114, 115, 116, 117, 117, 118, 119, 120, 121,
121, 122, 123, 124, 125, 125, 126, 127, 128, 129,
130, 130, 131, 132, 133, 134, 134, 135, 136, 137,
138, 138, 139, 140, 141, 142, 143, 143, 144, 145,
146, 147, 148, 148, 149, 150, 151, 152, 153, 153,
154, 155, 156, 157, 158, 159, 159, 160, 161, 162,
163, 164, 165, 165, 166, 167, 168, 169, 170, 171,
172, 172, 173, 174, 175, 176, 177, 178, 179, 180,
181, 182, 182, 183, 184, 185, 186, 187, 188, 189,
190, 191, 192, 193, 194, 195, 196, 197, 198, 199,
200, 201, 202, 203, 204, 205, 206, 207, 208, 209,
210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
220, 221, 223, 224, 225, 226, 227, 228, 230, 231,
232, 233, 234, 236, 237, 238, 240, 241, 242, 244,
245, 247, 249, 250, 252, 255
};

static const uint8_t ipl_gammaSigmoid0_9[256] = {
  0,   2,   3,   4,   6,   7,   8,   9,  11,  12,
 13,  14,  15,  16,  17,  19,  20,  21,  22,  23,
 24,  25,  26,  27,  28,  29,  30,  32,  33,  34,
 35,  36,  37,  38,  39,  40,  41,  42,  43,  44,
 45,  46,  47,  48,  49,  50,  51,  52,  53,  54,
 55,  56,  57,  58,  59,  60,  61,  62,  63,  64,
 65,  66,  67,  68,  69,  70,  70,  71,  72,  73,
 74,  75,  76,  77,  78,  79,  80,  81,  82,  83,
 84,  85,  86,  87,  88,  89,  89,  90,  91,  92,
 93,  94,  95,  96,  97,  98,  99, 100, 101, 102,
102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
112, 113, 113, 114, 115, 116, 117, 118, 119, 120,
121, 122, 123, 123, 124, 125, 126, 127, 128, 129,
130, 131, 132, 132, 133, 134, 135, 136, 137, 138,
139, 140, 141, 142, 142, 143, 144, 145, 146, 147,
148, 149, 150, 151, 152, 153, 153, 154, 155, 156,
157, 158, 159, 160, 161, 162, 163, 164, 165, 166,
166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
176, 177, 178, 179, 180, 181, 182, 183, 184, 185,
185, 186, 187, 188, 189, 190, 191, 192, 193, 194,
195, 196, 197, 198, 199, 200, 201, 202, 203, 204,
205, 206, 207, 208, 209, 210, 211, 212, 213, 214,
215, 216, 217, 218, 219, 220, 221, 222, 223, 225,
226, 227, 228, 229, 230, 231, 232, 233, 234, 235,
236, 238, 239, 240, 241, 242, 243, 244, 246, 247,
248, 249, 251, 252, 253, 255
};


const uint8_t ipl_gammaSigmoid1_0[256] = {
  0,   1,   2,   3,   4,   5,   6,   7,   8,   9,
 10,  11,  12,  13,  14,  15,  16,  17,  18,  19,
 20,  21,  22,  23,  24,  25,  26,  27,  28,  29,
 30,  31,  32,  33,  34,  35,  36,  37,  38,  39,
 40,  41,  42,  43,  44,  45,  46,  47,  48,  49,
 50,  51,  52,  53,  54,  55,  56,  57,  58,  59,
 60,  61,  62,  63,  64,  65,  66,  67,  68,  69,
 70,  71,  72,  73,  74,  75,  76,  77,  78,  79,
 80,  81,  82,  83,  84,  85,  86,  87,  88,  89,
 90,  91,  92,  93,  94,  95,  96,  97,  98,  99,
100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
120, 121, 122, 123, 124, 125, 126, 127, 128, 129,
130, 131, 132, 133, 134, 135, 136, 137, 138, 139,
140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
160, 161, 162, 163, 164, 165, 166, 167, 168, 169,
170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
180, 181, 182, 183, 184, 185, 186, 187, 188, 189,
190, 191, 192, 193, 194, 195, 196, 197, 198, 199,
200, 201, 202, 203, 204, 205, 206, 207, 208, 209,
210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
220, 221, 222, 223, 224, 225, 226, 227, 228, 229,
230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
240, 241, 242, 243, 244, 245, 246, 247, 248, 249,
250, 251, 252, 253, 254, 255
};


static const uint8_t ipl_gammaSigmoid1_2[256] = {
  0,   0,   1,   1,   2,   3,   3,   4,   5,   5,
  6,   7,   7,   8,   9,  10,  11,  11,  12,  13,
 14,  15,  15,  16,  17,  18,  19,  20,  21,  22,
 22,  23,  24,  25,  26,  27,  28,  29,  30,  31,
 32,  33,  34,  35,  36,  37,  38,  38,  39,  40,
 41,  42,  43,  44,  45,  46,  48,  49,  50,  51,
 52,  53,  54,  55,  56,  57,  58,  59,  60,  61,
 62,  63,  64,  65,  66,  67,  69,  70,  71,  72,
 73,  74,  75,  76,  77,  78,  79,  81,  82,  83,
 84,  85,  86,  87,  88,  90,  91,  92,  93,  94,
 95,  96,  98,  99, 100, 101, 102, 103, 104, 106,
107, 108, 109, 110, 111, 113, 114, 115, 116, 117,
119, 120, 121, 122, 123, 125, 126, 127, 128, 129,
130, 132, 133, 134, 135, 136, 138, 139, 140, 141,
142, 144, 145, 146, 147, 148, 149, 151, 152, 153,
154, 155, 156, 157, 159, 160, 161, 162, 163, 164,
165, 167, 168, 169, 170, 171, 172, 173, 174, 176,
177, 178, 179, 180, 181, 182, 183, 184, 185, 186,
188, 189, 190, 191, 192, 193, 194, 195, 196, 197,
198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
209, 210, 211, 212, 213, 214, 215, 216, 217, 217,
218, 219, 220, 221, 222, 223, 224, 225, 226, 227,
228, 229, 230, 231, 232, 233, 233, 234, 235, 236,
237, 238, 239, 240, 240, 241, 242, 243, 244, 244,
245, 246, 247, 248, 248, 249, 250, 250, 251, 252,
252, 253, 254, 254, 255, 255
};


static const uint8_t ipl_gammaSigmoid1_5[256] = {
  0,   0,   0,   0,   1,   1,   1,   2,   2,   2,
  3,   3,   4,   4,   5,   5,   6,   6,   7,   7,
  8,   9,   9,  10,  10,  11,  12,  12,  13,  14,
 15,  15,  16,  17,  18,  18,  19,  20,  21,  22,
 22,  23,  24,  25,  26,  27,  28,  29,  29,  30,
 31,  32,  33,  34,  35,  36,  37,  38,  39,  40,
 41,  42,  43,  44,  45,  46,  47,  49,  50,  51,
 52,  53,  54,  55,  56,  58,  59,  60,  61,  62,
 63,  65,  66,  67,  68,  69,  71,  72,  73,  74,
 76,  77,  78,  79,  81,  82,  83,  85,  86,  87,
 89,  90,  91,  93,  94,  95,  97,  98,  99, 101,
102, 104, 105, 106, 108, 109, 111, 112, 114, 115,
116, 118, 119, 121, 122, 124, 125, 127, 128, 130,
131, 133, 134, 136, 137, 139, 140, 141, 143, 144,
146, 147, 149, 150, 151, 153, 154, 156, 157, 158,
160, 161, 162, 164, 165, 166, 168, 169, 170, 172,
173, 174, 176, 177, 178, 179, 181, 182, 183, 184,
186, 187, 188, 189, 190, 192, 193, 194, 195, 196,
197, 199, 200, 201, 202, 203, 204, 205, 206, 208,
209, 210, 211, 212, 213, 214, 215, 216, 217, 218,
219, 220, 221, 222, 223, 224, 225, 226, 226, 227,
228, 229, 230, 231, 232, 233, 233, 234, 235, 236,
237, 237, 238, 239, 240, 240, 241, 242, 243, 243,
244, 245, 245, 246, 246, 247, 248, 248, 249, 249,
250, 250, 251, 251, 252, 252, 253, 253, 253, 254,
254, 254, 255, 255, 255, 255
};


static const uint8_t ipl_gammaSigmoid1_8[256] = {
  0,   0,   0,   0,   0,   0,   1,   1,   1,   1,
  1,   2,   2,   2,   2,   3,   3,   3,   4,   4,
  5,   5,   5,   6,   6,   7,   7,   8,   8,   9,
  9,  10,  11,  11,  12,  12,  13,  14,  14,  15,
 16,  17,  17,  18,  19,  20,  20,  21,  22,  23,
 24,  25,  25,  26,  27,  28,  29,  30,  31,  32,
 33,  34,  35,  36,  37,  38,  39,  40,  41,  42,
 43,  44,  46,  47,  48,  49,  50,  51,  53,  54,
 55,  56,  58,  59,  60,  61,  63,  64,  65,  67,
 68,  69,  71,  72,  74,  75,  77,  78,  79,  81,
 82,  84,  85,  87,  88,  90,  91,  93,  95,  96,
 98,  99, 101, 103, 104, 106, 108, 109, 111, 113,
114, 116, 118, 120, 121, 123, 125, 127, 128, 130,
132, 134, 135, 137, 139, 141, 142, 144, 146, 147,
149, 151, 152, 154, 156, 157, 159, 160, 162, 164,
165, 167, 168, 170, 171, 173, 174, 176, 177, 178,
180, 181, 183, 184, 186, 187, 188, 190, 191, 192,
194, 195, 196, 197, 199, 200, 201, 202, 204, 205,
206, 207, 208, 209, 211, 212, 213, 214, 215, 216,
217, 218, 219, 220, 221, 222, 223, 224, 225, 226,
227, 228, 229, 230, 230, 231, 232, 233, 234, 235,
235, 236, 237, 238, 238, 239, 240, 241, 241, 242,
243, 243, 244, 244, 245, 246, 246, 247, 247, 248,
248, 249, 249, 250, 250, 250, 251, 251, 252, 252,
252, 253, 253, 253, 253, 254, 254, 254, 254, 254,
255, 255, 255, 255, 255, 255
};


static const uint8_t ipl_gammaSigmoid2_1[256] = {
  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  1,   1,   1,   1,   1,   1,   2,   2,   2,   2,
  3,   3,   3,   3,   4,   4,   5,   5,   5,   6,
  6,   7,   7,   7,   8,   8,   9,   9,  10,  11,
 11,  12,  12,  13,  14,  14,  15,  16,  16,  17,
 18,  19,  19,  20,  21,  22,  23,  24,  24,  25,
 26,  27,  28,  29,  30,  31,  32,  33,  34,  35,
 36,  37,  38,  40,  41,  42,  43,  44,  45,  47,
 48,  49,  50,  52,  53,  54,  56,  57,  59,  60,
 61,  63,  64,  66,  67,  69,  70,  72,  73,  75,
 77,  78,  80,  81,  83,  85,  87,  88,  90,  92,
 94,  95,  97,  99, 101, 103, 105, 106, 108, 110,
112, 114, 116, 118, 120, 122, 124, 126, 129, 131,
133, 135, 137, 139, 141, 143, 145, 147, 149, 150,
152, 154, 156, 158, 160, 161, 163, 165, 167, 168,
170, 172, 174, 175, 177, 178, 180, 182, 183, 185,
186, 188, 189, 191, 192, 194, 195, 196, 198, 199,
201, 202, 203, 205, 206, 207, 208, 210, 211, 212,
213, 214, 215, 217, 218, 219, 220, 221, 222, 223,
224, 225, 226, 227, 228, 229, 230, 231, 231, 232,
233, 234, 235, 236, 236, 237, 238, 239, 239, 240,
241, 241, 242, 243, 243, 244, 244, 245, 246, 246,
247, 247, 248, 248, 248, 249, 249, 250, 250, 250,
251, 251, 252, 252, 252, 252, 253, 253, 253, 253,
254, 254, 254, 254, 254, 254, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255
};


static const uint8_t ipl_gammaSigmoid2_4[256] = {
  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  0,   0,   0,   1,   1,   1,   1,   1,   1,   1,
  1,   2,   2,   2,   2,   3,   3,   3,   3,   4,
  4,   4,   5,   5,   5,   6,   6,   7,   7,   7,
  8,   8,   9,   9,  10,  10,  11,  12,  12,  13,
 13,  14,  15,  16,  16,  17,  18,  18,  19,  20,
 21,  22,  23,  23,  24,  25,  26,  27,  28,  29,
 30,  31,  32,  33,  35,  36,  37,  38,  39,  40,
 42,  43,  44,  46,  47,  48,  50,  51,  52,  54,
 55,  57,  58,  60,  61,  63,  65,  66,  68,  69,
 71,  73,  75,  76,  78,  80,  82,  84,  86,  88,
 89,  91,  93,  95,  97, 100, 102, 104, 106, 108,
110, 112, 115, 117, 119, 122, 124, 126, 129, 131,
133, 136, 138, 140, 143, 145, 147, 149, 151, 153,
155, 158, 160, 162, 164, 166, 167, 169, 171, 173,
175, 177, 179, 180, 182, 184, 186, 187, 189, 190,
192, 194, 195, 197, 198, 200, 201, 203, 204, 205,
207, 208, 209, 211, 212, 213, 215, 216, 217, 218,
219, 220, 222, 223, 224, 225, 226, 227, 228, 229,
230, 231, 232, 232, 233, 234, 235, 236, 237, 237,
238, 239, 239, 240, 241, 242, 242, 243, 243, 244,
245, 245, 246, 246, 247, 247, 248, 248, 248, 249,
249, 250, 250, 250, 251, 251, 251, 252, 252, 252,
252, 253, 253, 253, 253, 254, 254, 254, 254, 254,
254, 254, 254, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255
};

static const uint8_t VFE_Whiteboard_GammaTable[1024] = {
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5,
  6, 6, 7, 7, 8, 8, 9, 9, 10, 11, 11, 12, 12, 13, 13, 14,
  14, 15, 16, 16, 17, 18, 19, 20, 20, 21, 22, 22, 23, 24, 24, 25,
  26, 27, 28, 28, 29, 30, 31, 32, 33, 34, 35, 35, 36, 37, 38, 39,
  40, 41, 42, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 53, 54, 55,
  56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 68, 69, 70, 71, 72,
  73, 74, 76, 77, 78, 79, 80, 81, 83, 84, 85, 86, 88, 89, 90, 91,
  92, 94, 95, 96, 97, 98, 100, 101, 102, 103, 104, 106, 107, 108, 109, 110,
  112, 113, 114, 115, 116, 117, 118, 119, 121, 122, 123, 124, 125, 126, 127,
  128,
  130, 131, 132, 133, 134, 135, 136, 137, 139, 140, 141, 142, 143, 144, 145,
  146,
  147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161,
  162,
  163, 164, 165, 166, 166, 167, 168, 169, 170, 171, 172, 173, 174, 174, 175,
  176,
  177, 178, 179, 179, 180, 181, 182, 183, 183, 184, 185, 185, 186, 187, 187,
  188,
  189, 190, 190, 191, 192, 192, 193, 194, 194, 195, 196, 196, 197, 198, 198,
  199,
  200, 201, 201, 202, 202, 203, 203, 204, 205, 205, 206, 207, 207, 207, 208,
  209,
  209, 209, 210, 210, 210, 211, 212, 212, 212, 213, 214, 214, 215, 215, 216,
  216,
  216, 217, 217, 218, 218, 219, 219, 220, 220, 221, 221, 221, 222, 222, 222,
  223,
  223, 224, 224, 224, 225, 225, 225, 226, 226, 227, 227, 227, 228, 228, 228,
  229,
  229, 230, 230, 230, 231, 231, 231, 231, 232, 232, 232, 233, 233, 234, 234,
  234,
  235, 235, 235, 235, 235, 236, 236, 236, 236, 237, 237, 237, 237, 238, 238,
  238,
  239, 239, 239, 240, 240, 240, 241, 241, 241, 241, 242, 242, 242, 242, 242,
  243,
  243, 243, 243, 244, 244, 244, 244, 244, 245, 245, 245, 245, 245, 246, 246,
  246,
  246, 246, 246, 246, 246, 246, 247, 247, 247, 247, 247, 248, 248, 248, 248,
  248,
  249, 249, 249, 249, 249, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250,
  251,
  251, 251, 251, 251, 252, 252, 252, 252, 252, 252, 252, 252, 252, 252, 253,
  253,
  253, 253, 253, 253, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
};

static const uint8_t VFE_Posterization_GammaTable[1024] = {
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 85, 85, 85, 85, 85, 85,
  85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85,
  85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85,
  85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85,
  85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85,
  85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85,
  85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85,
  85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85,
  85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85,
  85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85,
  85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85,
  85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 170, 170, 170, 170, 170,
  170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
  170,
  170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
  170,
  170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
  170,
  170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
  170,
  170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
  170,
  170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
  170,
  170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
  170,
  170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
  170,
  170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
  170,
  170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
  170,
  170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
  170,
  170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
  170,
  170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
  170,
  170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
  170,
  170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
  170,
  170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
  170,
  170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
  170,
  170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
  170,
  170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
  170,
  170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
  170,
  170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
  170,
  170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
  170,
  170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  255,
};

static const uint8_t VFE_Blackboard_GammaTable[1024] = {
  /* Gamma table */
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
  4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8,
  8, 9, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, 12, 13, 13,
  13, 14, 14, 14, 15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19,
  19, 20, 20, 20, 21, 21, 22, 22, 22, 23, 23, 23, 24, 24, 24, 24,
  25, 25, 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, 30, 30, 30, 31,
  31, 32, 32, 32, 33, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38,
  38, 39, 39, 39, 40, 40, 40, 41, 41, 42, 42, 42, 43, 43, 43, 44,
  44, 45, 45, 46, 46, 47, 47, 47, 48, 48, 48, 49, 49, 49, 50, 50,
  51, 51, 52, 52, 53, 53, 53, 54, 54, 54, 55, 55, 56, 56, 57, 57,
  58, 58, 58, 59, 59, 60, 60, 61, 61, 61, 62, 62, 63, 63, 63, 64,
  64, 65, 65, 66, 66, 66, 67, 67, 67, 68, 68, 69, 69, 69, 70, 70,
  71, 71, 71, 72, 72, 73, 73, 74, 74, 74, 75, 75, 76, 76, 77, 77,
  77, 78, 78, 79, 79, 79, 80, 80, 80, 81, 81, 81, 82, 82, 83, 83,
  83, 84, 84, 85, 85, 86, 86, 86, 87, 87, 87, 88, 88, 89, 89, 89,
  90, 90, 90, 91, 91, 92, 92, 92, 93, 93, 93, 94, 94, 95, 95, 95,
  96, 96, 96, 97, 97, 98, 98, 99, 99, 100, 100, 100, 101, 101, 101, 102,
  102, 102, 103, 103, 103, 104, 104, 104, 105, 105, 106, 106, 106, 107, 107,
  107,
  108, 108, 108, 109, 109, 110, 110, 111, 111, 111, 112, 112, 112, 113, 113,
  113,
  114, 114, 115, 115, 115, 116, 116, 116, 117, 117, 117, 118, 118, 118, 119,
  119,
  119, 120, 120, 120, 121, 121, 121, 122, 122, 122, 123, 123, 123, 124, 124,
  124,
  125, 125, 125, 126, 126, 126, 127, 127, 127, 128, 128, 128, 129, 129, 129,
  130,
  130, 130, 131, 131, 131, 132, 132, 132, 133, 133, 134, 134, 134, 135, 135,
  135,
  136, 136, 136, 136, 137, 137, 137, 138, 138, 138, 139, 139, 139, 140, 140,
  140,
  141, 141, 141, 141, 142, 142, 142, 143, 143, 143, 143, 144, 144, 145, 145,
  145,
  145, 146, 146, 147, 147, 147, 147, 148, 148, 148, 149, 149, 149, 149, 150,
  150,
  150, 150, 151, 151, 152, 152, 152, 152, 153, 153, 154, 154, 154, 154, 155,
  155,
  155, 155, 156, 156, 156, 156, 157, 157, 157, 157, 158, 158, 159, 159, 159,
  159,
  160, 160, 160, 161, 161, 161, 161, 162, 162, 163, 163, 163, 163, 164, 164,
  164,
  164, 165, 165, 165, 165, 165, 166, 166, 166, 166, 167, 167, 167, 168, 168,
  168,
  168, 169, 169, 170, 170, 170, 170, 171, 171, 171, 172, 172, 172, 172, 173,
  173,
  173, 173, 173, 174, 174, 174, 174, 174, 175, 175, 175, 175, 176, 176, 176,
  177,
  177, 177, 177, 178, 178, 178, 179, 179, 179, 179, 180, 180, 180, 181, 181,
  181,
  181, 182, 182, 182, 182, 183, 183, 183, 183, 184, 184, 184, 184, 184, 185,
  185,
  185, 185, 185, 185, 186, 186, 186, 186, 187, 187, 187, 188, 188, 188, 188,
  189,
  189, 189, 189, 190, 190, 190, 190, 191, 191, 191, 191, 192, 192, 192, 192,
  193,
  193, 193, 193, 194, 194, 194, 194, 195, 195, 195, 195, 196, 196, 196, 196,
  197,
  197, 197, 197, 197, 197, 198, 198, 198, 198, 198, 198, 198, 199, 199, 199,
  199,
  200, 200, 200, 200, 201, 201, 201, 201, 202, 202, 202, 202, 202, 203, 203,
  203,
  203, 204, 204, 204, 204, 205, 205, 205, 205, 206, 206, 206, 206, 206, 207,
  207,
  207, 207, 208, 208, 208, 208, 208, 209, 209, 209, 209, 210, 210, 210, 210,
  210,
  210, 210, 211, 211, 211, 211, 211, 211, 211, 211, 212, 212, 212, 212, 213,
  213,
  213, 213, 213, 214, 214, 214, 214, 215, 215, 215, 215, 215, 215, 216, 216,
  216,
  216, 217, 217, 217, 217, 217, 218, 218, 218, 218, 219, 219, 219, 219, 219,
  219,
  220, 220, 220, 220, 221, 221, 221, 221, 221, 221, 222, 222, 222, 222, 223,
  223,
  223, 223, 223, 223, 224, 224, 224, 224, 225, 225, 225, 225, 225, 225, 225,
  226,
  226, 226, 226, 227, 227, 227, 227, 227, 227, 227, 227, 228, 228, 228, 228,
  228,
  228, 228, 228, 228, 229, 229, 229, 229, 230, 230, 230, 230, 230, 230, 230,
  231,
  231, 231, 231, 232, 232, 232, 232, 232, 232, 232, 233, 233, 233, 233, 234,
  234,
  234, 234, 234, 234, 234, 235, 235, 235, 235, 236, 236, 236, 236, 236, 236,
  236,
  236, 237, 237, 237, 237, 238, 238, 238, 238, 238, 238, 238, 239, 239, 239,
  239,
  240, 240, 240, 240, 240, 240, 240, 240, 241, 241, 241, 241, 242, 242, 242,
  242,
  242, 242, 242, 242, 243, 243, 243, 243, 244, 244, 244, 244, 244, 244, 244,
  244,
  244, 244, 244, 245, 245, 245, 245, 245, 245, 245, 245, 245, 245, 246, 246,
  246,
  246, 247, 247, 247, 247, 247, 247, 247, 247, 247, 248, 248, 248, 248, 249,
  249,
  249, 249, 249, 249, 249, 249, 249, 250, 250, 250, 250, 251, 251, 251, 251,
  251,
  251, 251, 251, 251, 252, 252, 252, 252, 253, 253, 253, 253, 253, 253, 254,
  255,
};

/*  Y Gamma Config Command */
#ifdef VFE_2X
const VFE_Y_GammaConfigCmdType VFE_DefaultY_GammaCfgCmd = {
  VFE_GAMMA_PIECEWISE_LINEAR_MAPPING_OF_10_BIT_GAMMA_TABLE,
/* Luma Gamma table */
  {
    {0, 6, 8, 10}, {11, 12, 13, 14}, {15, 16, 17, 17}, {18, 19, 19, 20},
    {21, 21, 22, 22}, {23, 23, 24, 24}, {25, 25, 26, 26}, {27, 27, 27, 28},
    {28, 29, 29, 29}, {30, 30, 31, 31}, {31, 32, 32, 32}, {33, 33, 33, 34},
    {34, 34, 35, 35}, {35, 35, 36, 36}, {36, 37, 37, 37}, {38, 38, 38, 38},
    {39, 39, 39, 39}, {40, 40, 40, 41}, {41, 41, 41, 42}, {42, 42, 42, 43},
    {43, 43, 43, 44}, {44, 44, 44, 44}, {45, 45, 45, 45}, {46, 46, 46, 46},
    {46, 47, 47, 47}, {47, 48, 48, 48}, {48, 48, 49, 49}, {49, 49, 49, 50},
    {50, 50, 50, 50}, {51, 51, 51, 51}, {51, 52, 52, 52}, {52, 52, 53, 53},
    {53, 53, 53, 54}, {54, 54, 54, 54}, {54, 55, 55, 55}, {55, 55, 56, 56},
    {56, 56, 56, 56}, {57, 57, 57, 57}, {57, 57, 58, 58}, {58, 58, 58, 58},
    {59, 59, 59, 59}, {59, 59, 60, 60}, {60, 60, 60, 60}, {61, 61, 61, 61},
    {61, 61, 62, 62}, {62, 62, 62, 62}, {62, 63, 63, 63}, {63, 63, 63, 64},
    {64, 64, 64, 64}, {64, 64, 65, 65}, {65, 65, 65, 65}, {65, 66, 66, 66},
    {66, 66, 66, 66}, {67, 67, 67, 67}, {67, 67, 67, 68}, {68, 68, 68, 68},
    {68, 68, 69, 69}, {69, 69, 69, 69}, {69, 70, 70, 70}, {70, 70, 70, 70},
    {70, 71, 71, 71}, {71, 71, 71, 71}, {72, 72, 72, 72}, {72, 72, 72, 72},
    {73, 73, 73, 73}, {74, 74, 74, 74}, {75, 75, 75, 75}, {76, 76, 76, 76},
    {77, 77, 77, 77}, {78, 78, 78, 78}, {78, 79, 79, 79}, {79, 80, 80, 80},
    {80, 81, 81, 81}, {81, 81, 82, 82}, {82, 82, 83, 83}, {83, 83, 83, 84},
    {84, 84, 84, 85}, {85, 85, 85, 85}, {86, 86, 86, 86}, {86, 87, 87, 87},
    {87, 87, 88, 88}, {88, 88, 89, 89}, {89, 89, 89, 90}, {90, 90, 90, 90},
    {91, 91, 91, 91}, {91, 92, 92, 92}, {92, 92, 92, 93}, {93, 93, 93, 93},
    {94, 94, 94, 94}, {94, 95, 95, 95}, {95, 95, 96, 96}, {96, 96, 96, 96},
    {97, 97, 97, 97}, {97, 98, 98, 98}, {98, 98, 98, 99}, {99, 99, 99, 99},
    {99, 100, 100, 100}, {100, 100, 101, 101}, {101, 101, 101, 101}, {102,
      102,
      102,
      102},
    {102, 102, 103, 103}, {103, 103, 103, 103}, {104, 104, 104, 104}, {104,
      104,
      105,
      105},
    {105, 105, 105, 105}, {106, 106, 106, 106}, {106, 106, 107, 107}, {107,
      107,
      107,
      107},
    {108, 108, 108, 108}, {108, 108, 109, 109}, {109, 109, 109, 109}, {109,
      110,
      110,
      110},
    {110, 110, 110, 111}, {111, 111, 111, 111}, {111, 111, 112, 112}, {112,
      112,
      112,
      112},
    {113, 113, 113, 113}, {113, 113, 113, 114}, {114, 114, 114, 114}, {114,
      115,
      115,
      115},
    {115, 115, 115, 115}, {116, 116, 116, 116}, {116, 116, 116, 117}, {117,
      117,
      117,
      117},
    {117, 117, 118, 118}, {118, 118, 118, 118}, {118, 119, 119, 119}, {119,
      119,
      119,
      119},
    {120, 120, 120, 120}, {121, 121, 121, 122}, {122, 122, 122, 123}, {123,
      123,
      124,
      124},
    {124, 124, 125, 125}, {125, 125, 126, 126}, {126, 126, 127, 127}, {127,
      128,
      128,
      128},
    {128, 129, 129, 129}, {129, 130, 130, 130}, {130, 131, 131, 131}, {131,
      132,
      132,
      132},
    {132, 133, 133, 133}, {133, 134, 134, 134}, {134, 135, 135, 135}, {135,
      136,
      136,
      136},
    {136, 137, 137, 137}, {137, 138, 138, 138}, {138, 138, 139, 139}, {139,
      139,
      140,
      140},
    {140, 140, 141, 141}, {141, 141, 142, 142}, {142, 142, 142, 143}, {143,
      143,
      143,
      144},
    {144, 144, 144, 145}, {145, 145, 145, 145}, {146, 146, 146, 146}, {147,
      147,
      147,
      147},
    {147, 148, 148, 148}, {148, 148, 149, 149}, {149, 149, 150, 150}, {150,
      150,
      150,
      151},
    {151, 151, 151, 152}, {152, 152, 152, 152}, {153, 153, 153, 153}, {153,
      154,
      154,
      154},
    {154, 154, 155, 155}, {155, 155, 156, 156}, {156, 156, 156, 157}, {157,
      157,
      157,
      157},
    {158, 158, 158, 158}, {158, 159, 159, 159}, {159, 159, 160, 160}, {160,
      160,
      160,
      161},
    {161, 161, 161, 161}, {162, 162, 162, 162}, {162, 163, 163, 163}, {163,
      163,
      164,
      164},
    {164, 164, 165, 165}, {165, 166, 166, 167}, {167, 167, 168, 168}, {168,
      169,
      169,
      170},
    {170, 170, 171, 171}, {171, 172, 172, 173}, {173, 173, 174, 174}, {174,
      175,
      175,
      175},
    {176, 176, 177, 177}, {177, 178, 178, 178}, {179, 179, 179, 180}, {180,
      180,
      181,
      181},
    {181, 182, 182, 182}, {183, 183, 183, 184}, {184, 184, 185, 185}, {185,
      186,
      186,
      186},
    {187, 187, 187, 188}, {188, 188, 189, 189}, {189, 190, 190, 190}, {191,
      191,
      191,
      192},
    {192, 192, 193, 193}, {193, 194, 194, 194}, {195, 195, 195, 196}, {196,
      196,
      196,
      197},
    {197, 197, 198, 198}, {198, 199, 199, 199}, {200, 200, 200, 200}, {201,
      201,
      201,
      202},
    {202, 202, 203, 203}, {203, 203, 204, 204}, {204, 205, 205, 205}, {206,
      206,
      206,
      206},
    {207, 207, 207, 208}, {208, 208, 209, 209}, {209, 209, 210, 210}, {210,
      211,
      211,
      211},
    {211, 212, 212, 212}, {213, 213, 213, 213}, {214, 214, 214, 215}, {215,
      215,
      215,
      216},
    {216, 216, 217, 218}, {218, 219, 219, 220}, {220, 221, 221, 222}, {222,
      223,
      224,
      224},
    {225, 225, 226, 226}, {227, 227, 228, 228}, {229, 229, 230, 230}, {231,
      231,
      232,
      232},
    {233, 233, 234, 234}, {235, 235, 236, 236}, {237, 237, 238, 238}, {239,
      239,
      240,
      240},
    {241, 241, 242, 242}, {243, 243, 244, 244}, {245, 245, 246, 246}, {247,
      247,
      248,
      248},
    {249, 249, 250, 250}, {250, 251, 251, 252}, {252, 253, 253, 254}, {254,
      255,
      255,
      255},
    {255, 255, 255, 255}, {255, 255, 255, 255}, {255, 255, 255, 255}, {255,
      255,
      255,
      255},
    {255, 255, 255, 255}, {255, 255, 255, 255}, {255, 255, 255, 255}, {255,
      255,
      255,
      255},
    {255, 255, 255, 255}, {255, 255, 255, 255}, {255, 255, 255, 255}, {255,
      255,
      255,
      255},
    {255, 255, 255, 255}, {255, 255, 255, 255}, {255, 255, 255, 255}, {255,
      255,
      255,
      255},
    {255, 255, 255, 255}, {255, 255, 255, 255}, {255, 255, 255, 255}, {255,
      255,
      255,
      255}
  }
};
#endif

#define PRINT_TABLE(table) ({ \
  int i; \
  CDBG("gamma table from chromatix"); \
  for(i=0; i<GAMMA_TABLE_SIZE/16; i++) \
    CDBG("%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d", \
      table[i], table[i+1], table[i+2], table[i+3], table[i+4], \
      table[i+5], table[i+6], table[i+7], table[i+8], \
      table[i+9], table[i+10], table[i+11], table[i+12], \
      table[i+13], table[i+14], table[i+15]); })

/*===========================================================================
 * FUNCTION    - gamma_update_tab_on_contrast -
 *
 * DESCRIPTION:
 *==========================================================================*/
int8_t gamma_update_tab_on_contrast(int contrast, int ibits,
  uint8_t*  input_table, uint8_t*  output_table)
{
  int i;
  int entries;
  const uint8_t * gamma_table;
  entries = (ibits >= 0) ? (1 << ibits) : 256;

  /* Apply Sigmoid Gamma Table */
  if (input_table == NULL || output_table == NULL) {
    CDBG("%s: input improper\n", __func__);
    return FALSE;
  }

  switch (contrast) {
    case 10:
      gamma_table = ipl_gammaSigmoid2_4;
    break;
    case 9:
      gamma_table = ipl_gammaSigmoid2_1;
    break;
    case 8:
      gamma_table = ipl_gammaSigmoid1_8;
    break;
    case 7:
      gamma_table = ipl_gammaSigmoid1_5;
    break;
    case 6:
      gamma_table = ipl_gammaSigmoid1_2;
    break;
    case 5:
      /* the input and output table are the same */
      for (i=0; i<entries; i++)
        output_table[i] = input_table[i];
      return TRUE;
    case 4:
      gamma_table = ipl_gammaSigmoid0_9;
    break;
    case 3:
      gamma_table = ipl_gammaSigmoid0_8;
    break;
    case 2:
      gamma_table = ipl_gammaSigmoid0_7;
    break;
    case 1:
      gamma_table = ipl_gammaSigmoid0_6;
    break;
    case 0:
      gamma_table = ipl_gammaSigmoid0_5;
    break;
    default:
      CDBG(" %s: invalid contrast\n", __func__);
      return FALSE;
  }

  /* do look up */
  for (i = 0; i < entries; i++) {
    output_table[i] = gamma_table[input_table[i]];
  }

  CDBG("%s: success\n", __func__);

  return TRUE;
} /* gamma_update_tab_on_contrast */

/*===========================================================================
 * FUNCTION    - vfe_gamma_trigger_update -
 *
 * DESCRIPTION:
 *==========================================================================*/
vfe_status_t vfe_gamma_trigger_update(int mod_id, void *mod_g, void *vparms)
{
  vfe_status_t status = VFE_SUCCESS;
  int i;
  uint8_t *default_gamma, *lowlight_gamma, *outdoor_gamma;
  trigger_ratio_t trigger_ratio;
  trigger_point_type  *lowlight = NULL, *outdoor = NULL;
  gamma_mod_t* mod = (gamma_mod_t*)mod_g;
  vfe_params_t *parms = (vfe_params_t *)vparms;
  chromatix_parms_type *pchromatix = parms->chroma3a;
  int8_t is_snap = IS_SNAP_MODE(parms);
  int update_gamma = FALSE;
  tuning_control_type control_gamma = pchromatix->control_gamma;
  int index = (is_snap) ? SNAP : PREV;
  int backlight_comp_update = FALSE;

  CDBG("%s: enable %d", __func__, mod->enable);
  if (!mod->enable) {
    CDBG("%s: gamma not enabled", __func__);
    return VFE_SUCCESS;
  }

  if (!mod->trigger_enable) {
    CDBG("%s: gamma trigger not enabled", __func__);
    return VFE_SUCCESS;
  }

  if (parms->bs_mode != CAMERA_BESTSHOT_OFF) {
    CDBG("%s: Best shot enabled, skip trigger", __func__);
    return VFE_SUCCESS;
  }

  switch(parms->effects_params.spl_effect) {
  case CAMERA_EFFECT_POSTERIZE:
  case CAMERA_EFFECT_SOLARIZE:
  case CAMERA_EFFECT_WHITEBOARD:
  case CAMERA_EFFECT_BLACKBOARD:
    CDBG("%s: Special effect applied skip trigger", __func__);
    return VFE_SUCCESS;
  }

  if (!is_snap && !vfe_util_aec_check_settled(&parms->aec_params)) {
    CDBG("%s: AEC not settled", __func__);
    return VFE_SUCCESS;
  }

  if(!is_snap) {
    lowlight = &(pchromatix->gamma_lowlight_trigger);
    outdoor = &(pchromatix->gamma_outdoor_trigger);
    default_gamma = pchromatix->chromatix_rgb_default_gamma_table.gamma;
    lowlight_gamma = pchromatix->chromatix_rgb_yhi_ylo_gamma_table.gamma;
    outdoor_gamma = pchromatix->chromatix_rgb_outdoor_gamma_table.gamma;
  } else {
    lowlight = &(pchromatix->gamma_snapshot_lowlight_trigger);
    outdoor = &(pchromatix->gamma_snapshot_outdoor_trigger);
    default_gamma = pchromatix->chromatix_rgb_default_gamma_table_snapshot.gamma;
    lowlight_gamma = pchromatix->chromatix_rgb_yhi_ylo_gamma_table_snapshot.gamma;
    outdoor_gamma = pchromatix->chromatix_rgb_outdoor_gamma_table_snapshot.gamma;
  }

  trigger_ratio = vfe_util_get_aec_ratio2(control_gamma, outdoor, lowlight, parms);

  uint8_t *p_gamma_backlight = pchromatix->chromatix_rgb_backlight_gamma_table.gamma;
  backlight_comp_update = mod->enable_backlight_compensation &&
    parms->asd_params.backlight_detected &&
    (mod->backlight_severity != parms->asd_params.backlight_scene_severity);
  CDBG("%s: bklight %d severity %d", __func__, backlight_comp_update,
     parms->asd_params.backlight_scene_severity);

  update_gamma =
    ((mod->cur_mode != parms->vfe_op_mode) ||
    !F_EQUAL(trigger_ratio.ratio, mod->gamma_ratio.ratio)) ||
    backlight_comp_update ||
    mod->reload_params;
  CDBG("%s: update %d ratio %f lighting %d", __func__, update_gamma,
     trigger_ratio.ratio, trigger_ratio.lighting);

#ifdef VFE_2X
  update_gamma = update_gamma &&
    (trigger_ratio.lighting != mod->gamma_ratio.lighting);
#else
  update_gamma = update_gamma ||
    (trigger_ratio.lighting != mod->gamma_ratio.lighting);
#endif
  if (update_gamma) {
    mod->p_gamma_table[index] = mod->gamma_table[index];
    if (trigger_ratio.lighting == TRIGGER_OUTDOOR)
      TBL_INTERPOLATE_INT(default_gamma, outdoor_gamma, mod->p_gamma_table[index],
        trigger_ratio.ratio, GAMMA_TABLE_SIZE, i);
    else if (trigger_ratio.lighting == TRIGGER_LOWLIGHT)
      TBL_INTERPOLATE_INT(default_gamma, lowlight_gamma, mod->p_gamma_table[index],
        trigger_ratio.ratio, GAMMA_TABLE_SIZE, i);
    else
      memcpy(mod->p_gamma_table[index], default_gamma, GAMMA_TABLE_SIZE);

    if (backlight_comp_update) {
      float bl_ratio = (float)parms->asd_params.backlight_scene_severity/255.0;
      CDBG("%s: bl_ratio %f", __func__, bl_ratio);
      bl_ratio = MAX(1.0, bl_ratio);
      bl_ratio = MIN(0.0, bl_ratio);
      TBL_INTERPOLATE_INT(p_gamma_backlight, mod->p_gamma_table[index],
        mod->p_gamma_table[index], bl_ratio, GAMMA_TABLE_SIZE, i);
      mod->backlight_severity = parms->asd_params.backlight_scene_severity;
    }
    mod->gamma_ratio = trigger_ratio;
    mod->cur_mode = parms->vfe_op_mode;
    mod->update = TRUE;
    mod->trigger_update = TRUE;
    mod->reload_params = FALSE;
  }
  CDBG("%s: Update gamma %d status %d", __func__, update_gamma, status);
  return status;
} /* vfe_gamma_trigger_update */

/*===========================================================================
FUNCTION      gamma_update_vfe_table

DESCRIPTION
===========================================================================*/
#ifndef VFE_2X
void gamma_update_vfe_table(volatile VFE_GammaConfigCmdType *
  gamma_config_cmd, const uint8_t *table)
{
  int i;
  uint8_t diff;
  int16_t newSettings;

  /* this is the ratio between number of LUT entries in Tuning data vs number of
     actual VFE gamma LUT entries. */
  const int skipRatio = GAMMA_TABLE_SIZE / VFE_GAMMA_NUM_ENTRIES;
  CDBG("%s: skipRatio= %d\n", __func__, skipRatio);

  for (i = 0; i < VFE_GAMMA_NUM_ENTRIES - 1; i++) {
    /* The higher 8 bits in the configuration contains the delta between the
       current GammaTable value and the next value, while the lower 8 bits
       contains the current GammaTable value */

    newSettings = table[skipRatio*(i+1)] - table[skipRatio * i];
    newSettings = (int16_t)(newSettings << 8) + table[skipRatio * i];
    gamma_config_cmd->Gamatbl.table[i] = newSettings;
  }
  /* this is effectively  table[1023] - table[1008];
     this part is the delta */
  newSettings = table[1023] -
    table[(VFE_GAMMA_NUM_ENTRIES - 1) * skipRatio];
  /* scale the delta */
  newSettings = newSettings * (float)(16/15);
  /* form the value:  upper byte is delta,
     lower byte is the entry itself. */
  newSettings = (int16_t)(newSettings << 8) +
    table[skipRatio * (VFE_GAMMA_NUM_ENTRIES - 1)];

  gamma_config_cmd->Gamatbl.table[VFE_GAMMA_NUM_ENTRIES - 1] =
    newSettings;
} /* gamma_update_vfe_table */
#else /*7x targets*/
void gamma_update_vfe_table(volatile VFE_GammaConfigCmdType *
  gamma_config_cmd, const uint8_t *table)
{
  CDBG("%s: enter", __func__);
  int i, j;
  j = 0;
  for (i = 0; i < VFE_GAMMA_NUM_ENTRIES; i++) {
    gamma_config_cmd->Gamatbl.table[i] = 0;
    gamma_config_cmd->Gamatbl.table[i] =  table[j] | (table[j+1] << 8) | (table[j+2] << 16) | (table[j+3] << 24);
    j = j+4;

  }
  /* Update the mode */
  gamma_config_cmd->gammaMappingMode =
  //VFE_GAMMA_PIECEWISE_LINEAR_MAPPING_OF_10_BIT_GAMMA_TABLE;
  VFE_GAMMA_LINEAR_MAPPING_OF_10_BIT_GAMMA_TABLE;
} /* gamma_update_vfe_table */
#endif

/*===========================================================================
FUNCTION      gamma_get_vfe_table

DESCRIPTION
===========================================================================*/
void gamma_get_vfe_table(const uint8_t *table, int16_t *out_table)
{
  int i;
  uint8_t diff;
  int16_t newSettings;
  int16_t gamma_entries;

#ifndef VFE_2X
gamma_entries = VFE_GAMMA_NUM_ENTRIES;
#else
gamma_entries = 64;
#endif
  /* this is the ratio between number of LUT entries in Tuning data vs number of
     actual VFE gamma LUT entries. */
  const int skipRatio = GAMMA_TABLE_SIZE / gamma_entries;
  CDBG("%s: skipRatio= %d\n", __func__, skipRatio);

  for (i = 0; i < gamma_entries - 1; i++) {
    /* The higher 8 bits in the configuration contains the delta between the
       current GammaTable value and the next value, while the lower 8 bits
       contains the current GammaTable value */

    newSettings = table[skipRatio*(i+1)] - table[skipRatio * i];
    newSettings = (int16_t)(newSettings << 8) + table[skipRatio * i];
    out_table[i] = newSettings;
  }
  /* this is effectively  table[1023] - table[1008];
     this part is the delta */
  newSettings = table[1023] -
    table[(gamma_entries - 1) * skipRatio];
  /* scale the delta */
  newSettings = newSettings * (float)(16/15);
  /* form the value:  upper byte is delta,
     lower byte is the entry itself. */
  newSettings = (int16_t)(newSettings << 8) +
    table[skipRatio * (gamma_entries - 1)];

  out_table[gamma_entries - 1] = newSettings;
} /* gamma_update_vfe_table */

/*===========================================================================
 * FUNCTION    - vfe_gamma_config -
 *
 * DESCRIPTION:
 *==========================================================================*/
vfe_status_t vfe_gamma_config(int mod_id, void *mod_g, void *vparms)
{
  vfe_status_t status = VFE_SUCCESS;
  gamma_mod_t* mod = (gamma_mod_t*)mod_g;
  vfe_params_t *params = (vfe_params_t *)vparms;
  int is_snap = IS_SNAP_MODE(params);
  int index = (is_snap) ? SNAP : PREV;
  chromatix_parms_type *chromatix_ptr = params->chroma3a;
  uint8_t final_gamma_table[GAMMA_TABLE_SIZE];
  uint8_t* p_gamma_table = mod->p_gamma_table[index];

  CDBG("%s: Gamma Config update %d contrast %d gamma_type %d",
    __func__, mod->update, mod->contrast, mod->gamma_table_type);

  if (!mod->enable) {
    CDBG("%s: Gamma not enabled", __func__);
    return VFE_SUCCESS;
  }
  if (params->bs_mode == CAMERA_BESTSHOT_OFF) {
    /* compensate for contrast */
    switch(mod->gamma_table_type) {
      case GAMMA_TABLE_SOLARIZE:
      case GAMMA_TABLE_POSTERIZE:
      case GAMMA_TABLE_BACKLIGHT: {
        CDBG("%s: Dont apply contrast", __func__);
        break;
      }
      default: {
        CDBG("%s: Apply contrast %d", __func__, mod->contrast);
        if (mod->contrast != DEFAULT_CONTRAST) {
          gamma_update_tab_on_contrast(mod->contrast, 10, mod->p_gamma_table[index],
            final_gamma_table);
          p_gamma_table = final_gamma_table;
        }
      }
    }
    /* apply backlight compensation */

  }

  gamma_update_vfe_table(&mod->VFE_GammaCfgCmd, p_gamma_table);
#ifndef VFE_2X
  mod->VFE_GammaCfgCmd.LutSel.ch0BankSelect = 0;
  mod->VFE_GammaCfgCmd.LutSel.ch1BankSelect = 0;
  mod->VFE_GammaCfgCmd.LutSel.ch2BankSelect = 0;
#else
  mod->VFE_GammaCfgCmd.gammaMappingMode =
    VFE_GAMMA_LINEAR_MAPPING_OF_10_BIT_GAMMA_TABLE;
    //VFE_GAMMA_PIECEWISE_LINEAR_MAPPING_OF_10_BIT_GAMMA_TABLE;
#endif

  status = vfe_util_write_hw_cmd(params->camfd, CMD_GENERAL,
    (void *)&mod->VFE_GammaCfgCmd ,
    sizeof(mod->VFE_GammaCfgCmd), VFE_CMD_RGB_G_CFG);

#ifdef VFE_2X
  if (VFE_SUCCESS != status) {
    CDBG_HIGH("%s: RGB gamma config failed", __func__);
  }
  status = vfe_util_write_hw_cmd(params->camfd, CMD_GENERAL,
    (void *)&mod->VFE_Y_GammaCfgCmd ,
    sizeof(mod->VFE_Y_GammaCfgCmd), VFE_CMD_Y_GAMMA_CONFIG);
#endif

  if (VFE_SUCCESS == status) {
    mod->update = FALSE;
    mod->trigger_update = FALSE;
  }
  return status;
} /* vfe_gamma_snapshot_config */

/*===========================================================================
 * FUNCTION    - vfe_gamma_set_table -
 *
 * DESCRIPTION:
 *==========================================================================*/
vfe_status_t vfe_gamma_set_table(gamma_mod_t* mod, vfe_params_t *parms,
  vfe_gamma_table_t gamma_table_type)
{
  vfe_status_t status = VFE_SUCCESS;
  chromatix_parms_type *chromatix_ptr = parms->chroma3a;

  CDBG("%s: gamma_table_type %d", __func__, gamma_table_type);

  switch (gamma_table_type) {
    case GAMMA_TABLE_OUTDOOR:
      mod->p_gamma_table[PREV] =
        chromatix_ptr->chromatix_rgb_outdoor_gamma_table.gamma;
      mod->p_gamma_table[SNAP] =
        chromatix_ptr->chromatix_rgb_outdoor_gamma_table_snapshot.gamma;
      break;
    case GAMMA_TABLE_LOWLIGHT:
      mod->p_gamma_table[PREV] =
        chromatix_ptr->chromatix_rgb_yhi_ylo_gamma_table.gamma;
      mod->p_gamma_table[SNAP] =
        chromatix_ptr->chromatix_rgb_yhi_ylo_gamma_table_snapshot.gamma;
      break;
    case GAMMA_TABLE_BACKLIGHT:
      mod->p_gamma_table[PREV] =
        chromatix_ptr->chromatix_rgb_backlight_gamma_table.gamma;
      mod->p_gamma_table[SNAP] =
        chromatix_ptr->chromatix_rgb_backlight_gamma_table.gamma;
      break;
    case GAMMA_TABLE_SOLARIZE:
      mod->p_gamma_table[PREV] = mod->solarize_gamma_table;
      mod->p_gamma_table[SNAP] = mod->solarize_gamma_table;
      break;
    case GAMMA_TABLE_POSTERIZE:
      mod->p_gamma_table[PREV] = (uint8_t *)VFE_Posterization_GammaTable;
      mod->p_gamma_table[SNAP] = (uint8_t *)VFE_Posterization_GammaTable;
      break;
    case GAMMA_TABLE_WHITE_BOARD:
      mod->p_gamma_table[PREV] = (uint8_t *)VFE_Whiteboard_GammaTable;
      mod->p_gamma_table[SNAP] = (uint8_t *)VFE_Whiteboard_GammaTable;
      break;
    case GAMMA_TABLE_BLACK_BOARD:
      mod->p_gamma_table[PREV] = (uint8_t *)VFE_Blackboard_GammaTable;
      mod->p_gamma_table[SNAP] = (uint8_t *)VFE_Blackboard_GammaTable;
      break;
    case GAMMA_TABLE_DEFAULT:
    default:
      mod->p_gamma_table[PREV] =
        chromatix_ptr->chromatix_rgb_default_gamma_table.gamma;
      mod->p_gamma_table[SNAP] =
        chromatix_ptr->chromatix_rgb_default_gamma_table_snapshot.gamma;
      break;
  }
  mod->gamma_table_type = gamma_table_type;
  mod->update = TRUE;
  return status;
} /* vfe_gamma_set_table */

/*===========================================================================
 * FUNCTION    - vfe_gamma_set_bestshot -
 *
 * DESCRIPTION:
 *==========================================================================*/
vfe_status_t vfe_gamma_set_bestshot(int mod_id, void *mod_g, void *parms,
  camera_bestshot_mode_type mode)
{
  vfe_status_t status = VFE_SUCCESS;
  CDBG("%s: mode %d", __func__, mode);
  gamma_mod_t* mod = (gamma_mod_t*)mod_g;
  vfe_params_t *params = (vfe_params_t *)parms;

  mod->trigger_enable = FALSE;
  switch(mode) {
    case CAMERA_BESTSHOT_LANDSCAPE:
    case CAMERA_BESTSHOT_SNOW:
    case CAMERA_BESTSHOT_BEACH:
    case CAMERA_BESTSHOT_SPORTS:
    case CAMERA_BESTSHOT_ACTION:
      status = vfe_gamma_set_table(mod, params, GAMMA_TABLE_OUTDOOR);
      break;
    case CAMERA_BESTSHOT_NIGHT:
    case CAMERA_BESTSHOT_FIREWORKS:
    case CAMERA_BESTSHOT_NIGHT_PORTRAIT:
      status = vfe_gamma_set_table(mod, params, GAMMA_TABLE_LOWLIGHT);
      break;
    case CAMERA_BESTSHOT_BACKLIGHT:
      status = vfe_gamma_set_table(mod, params, GAMMA_TABLE_BACKLIGHT);
      break;
    default:
      status = vfe_gamma_set_table(mod, params, GAMMA_TABLE_DEFAULT);
      mod->trigger_enable = TRUE;
      break;
  }
  if (VFE_SUCCESS == status){
    CDBG("%s: enableupdate through BSM for mode : %d",__func__, mode);
    mod->update = TRUE;
  }
  return VFE_SUCCESS;
} /* vfe_gamma_set_bestshot */

/*===========================================================================
 * FUNCTION    - vfe_gamma_update -
 *
 * DESCRIPTION:
 *==========================================================================*/
vfe_status_t vfe_gamma_update(int mod_id, void *mod_g, void *vparms)
{
  vfe_status_t status = VFE_SUCCESS;
  gamma_mod_t* mod = (gamma_mod_t*)mod_g;
  vfe_params_t *parms = (vfe_params_t *)vparms;

  int is_snap = IS_SNAP_MODE(parms);
  int index = (is_snap) ? SNAP : PREV;
  VFE_GammaConfigCmdType *pcmd =  &mod->VFE_GammaCfgCmd;
  uint8_t final_gamma_table[GAMMA_TABLE_SIZE];
  uint8_t* p_gamma_table = mod->p_gamma_table[index];

#ifndef VFE_2X
  if (mod->hw_enable) {
    CDBG("%s: Update hardware", __func__);
    status = vfe_util_write_hw_cmd(parms->camfd,
      CMD_GENERAL, parms->moduleCfg,
      sizeof(VFE_ModuleCfgPacked),
      VFE_CMD_MODULE_CFG);
    if (status != VFE_SUCCESS) {
      CDBG_ERROR("%s: VFE_CMD_MODULE_CFG failed", __func__);
      return status;
    }
    parms->update |= VFE_MOD_GAMMA;
    mod->hw_enable = FALSE;
  }
#endif

  CDBG("%s: update %d\n",__func__, mod->update);
  if (!mod->enable) {
    CDBG("%s: gamma not enabled", __func__);
    return VFE_SUCCESS;
  }
  if (!mod->update) {
    CDBG("%s: gamma not updated", __func__);
    return VFE_SUCCESS;
  }

  /* update based on contrast */
  if (parms->bs_mode == CAMERA_BESTSHOT_OFF) {
    switch(mod->gamma_table_type) {
      case GAMMA_TABLE_SOLARIZE:
      case GAMMA_TABLE_POSTERIZE:
      case GAMMA_TABLE_BACKLIGHT: {
        CDBG("%s: Dont apply contrast", __func__);
        break;
      }
      default: {
        CDBG("%s: Apply contrast %d trig_update %d", __func__, mod->contrast,
          mod->trigger_update);
        if (mod->contrast != DEFAULT_CONTRAST) {
          gamma_update_tab_on_contrast(mod->contrast, 10, mod->p_gamma_table[index],
            final_gamma_table);
          p_gamma_table = final_gamma_table;
        }
      }
    }
  }

  gamma_update_vfe_table(pcmd, p_gamma_table);

#ifndef VFE_2X
  status = vfe_util_write_hw_cmd(parms->camfd, CMD_GENERAL, (void *)pcmd,
    sizeof(VFE_GammaConfigCmdType), VFE_CMD_RGB_G_UPDATE);

  if (VFE_SUCCESS == status) {
    mod->update = FALSE;
    mod->trigger_update = FALSE;
    parms->update |= VFE_MOD_GAMMA;
  }
#else
  CDBG("%s: set vfe_reconfig to 1\n", __func__);
  mod->update = FALSE;
  mod->reload_params = FALSE;
  if (parms->eztune_status)
    mod->vfe_reconfig = 1;
  else
    mod->vfe_reconfig = 0;
#endif
  return status;
} /* vfe_gamma_update */

vfe_status_t is_vfe_gamma_reconfig(vfe_ctrl_info_t *p_obj, void *parm)
{
  uint32_t *ptr = (uint32_t *)parm;
  *ptr = p_obj->vfe_module.gamma_mod.vfe_reconfig;
  p_obj->vfe_module.gamma_mod.vfe_reconfig = 0;
  return VFE_SUCCESS;
}
/*===========================================================================
 * Function:           vfe_gamma_enable
 *
 * Description:
 *=========================================================================*/
vfe_status_t vfe_gamma_enable(int mod_id, void *mod_g, void *vparms,
  int8_t g_enable, int8_t hw_write)
{
  vfe_status_t status = VFE_SUCCESS;
  gamma_mod_t* mod = (gamma_mod_t*)mod_g;
  vfe_params_t *params = (vfe_params_t *)vparms;

  if (!IS_BAYER_FORMAT(params))
    g_enable = FALSE;
  params->moduleCfg->rgbLUTEnable = g_enable;

  if (hw_write && (mod->enable == g_enable))
    return VFE_SUCCESS;

  mod->enable = g_enable;
  mod->hw_enable = hw_write;
  if (hw_write) {
    params->current_config = (g_enable) ?
      (params->current_config | VFE_MOD_GAMMA)
      : (params->current_config & ~VFE_MOD_GAMMA);
  }
  return status;
} /* vfe_gamma_set */

/*===========================================================================
 * FUNCTION    - gamma_set_solarize -
 *
 * DESCRIPTION:
 *==========================================================================*/
void gamma_set_solarize_table(gamma_mod_t *mod,
  chromatix_parms_type *chromatix_ptr)
{
  int i;
  uint16_t solarize_reflection_point =
    chromatix_ptr->chromatix_solarize_reflection_point;
  uint8_t* Gamma_Solarize = mod->solarize_gamma_table;
  uint8_t *Gamma_Lut =
    chromatix_ptr->chromatix_rgb_default_gamma_table.gamma;
  CDBG("%s: E", __func__);
  /* Chromatix reflection point is based on 256 (8 bit) gamma entries,
   * whereas VFE gamma has 1024 (10 bit) entries. So we multiple the
   * reflection point by 4 here */
  solarize_reflection_point <<= 2;
  memcpy(Gamma_Solarize, Gamma_Lut, solarize_reflection_point-1);

  for (i = solarize_reflection_point - 1; i < GAMMA_TABLE_SIZE; ++i)
    Gamma_Solarize[i] =
      ((255-Gamma_Lut[i])*Gamma_Lut[solarize_reflection_point-1] /
      (255-Gamma_Lut[solarize_reflection_point-1]));
}

/*===========================================================================
 * FUNCTION    - vfe_gamma_set_effect -
 *
 * DESCRIPTION:
 *==========================================================================*/
vfe_status_t vfe_gamma_set_spl_effect(int mod_id, void *mod_g, void *vparms,
  vfe_spl_effects_type type)
{
  vfe_status_t status = VFE_SUCCESS;
  gamma_mod_t* mod = (gamma_mod_t*)mod_g;
  vfe_params_t *parms = (vfe_params_t *)vparms;

  chromatix_parms_type *chromatix_ptr = parms->chroma3a;

  if (parms->bs_mode != CAMERA_BESTSHOT_OFF) {
    CDBG("%s: Best shot enabled, skip seteffect", __func__);
    return VFE_SUCCESS;
  }

  CDBG("%s: contrast %d effect type %d", __func__, mod->contrast, type);
  switch(type) {
    case CAMERA_EFFECT_POSTERIZE:
    case CAMERA_EFFECT_SOLARIZE: {
      /* UI should prevent this */
      CDBG_ERROR("%s: Warning Overriding contrast value to default",
        __func__);
      mod->contrast = DEFAULT_CONTRAST;
      parms->effects_params.contrast = .5;
    }
    default:;
  }
  CDBG("%s: type %d", __func__, type);
  switch (type) {
    case CAMERA_EFFECT_POSTERIZE:
     status = vfe_gamma_set_table(mod, parms, GAMMA_TABLE_POSTERIZE);
      break;
      case CAMERA_EFFECT_SOLARIZE:
     status = vfe_gamma_set_table(mod, parms, GAMMA_TABLE_SOLARIZE);
      break;
    case CAMERA_EFFECT_BLACKBOARD:
     status = vfe_gamma_set_table(mod, parms, GAMMA_TABLE_BLACK_BOARD);
      break;
    case CAMERA_EFFECT_WHITEBOARD:
     status = vfe_gamma_set_table(mod, parms, GAMMA_TABLE_WHITE_BOARD);
      break;
    default:
     status = vfe_gamma_set_table(mod, parms, GAMMA_TABLE_DEFAULT);
      break;
  }
  if (VFE_SUCCESS == status)
    mod->update = TRUE;
  return status;
} /* vfe_gamma_set_effect */

/*===========================================================================
 * FUNCTION    - vfe_gamma_get_table -
 *
 * DESCRIPTION:
 *==========================================================================*/
vfe_status_t vfe_gamma_get_table(gamma_mod_t* mod, vfe_params_t *parms,
  uint32_t *size, int16_t **pp_out_table)
{
  vfe_status_t status = VFE_SUCCESS;
  chromatix_parms_type *chromatix_ptr = parms->chroma3a;
  int is_snap = IS_SNAP_MODE(parms);
  int index = (is_snap) ? 1 : 0;
  uint8_t final_gamma_table[GAMMA_TABLE_SIZE];
  uint8_t* p_gamma_table = mod->p_gamma_table[index];

  if (!mod->enable) {
    CDBG("%s: gamma not enabled", __func__);
    *pp_out_table = NULL;
    *size = 0;
    return status;
  }
  CDBG("%s: type %d", __func__, mod->gamma_table_type);

  *pp_out_table = (int16_t *)malloc(VFE_GAMMA_NUM_ENTRIES * sizeof(int16_t));
  if (NULL == *pp_out_table) {
    CDBG_ERROR("%s: No memory", __func__);
    *pp_out_table = NULL;
    *size = 0;
    return VFE_ERROR_NO_MEMORY;
  }
  if (mod->contrast != DEFAULT_CONTRAST) {
    gamma_update_tab_on_contrast(mod->contrast, 10, mod->p_gamma_table[index],
      final_gamma_table);
    p_gamma_table = final_gamma_table;
  }
  gamma_get_vfe_table(p_gamma_table, *pp_out_table);
#ifndef VFE_2X
*size = VFE_GAMMA_NUM_ENTRIES;
#else
*size = 64;
#endif
  return status;
}

/*===========================================================================
 * FUNCTION    - vfe_gamma_set_contrast -
 *
 * DESCRIPTION:
 *==========================================================================*/
vfe_status_t vfe_gamma_set_contrast(int mod_id, void *mod_g, void *vparms,
  int32_t contrast)
{
  vfe_status_t status = VFE_SUCCESS;
  gamma_mod_t* mod = (gamma_mod_t*)mod_g;
  vfe_params_t *params = (vfe_params_t *)vparms;

  CDBG("%s: bs_mode %d", __func__, params->bs_mode);
  if (params->bs_mode != CAMERA_BESTSHOT_OFF) {
    CDBG("%s: Best shot enabled, ignore contast", __func__);
    return VFE_ERROR_INVALID_OPERATION;
  }
  CDBG("%s: contrast %d type %d", __func__, contrast, mod->gamma_table_type);
  switch(mod->gamma_table_type) {
    case GAMMA_TABLE_SOLARIZE:
    case GAMMA_TABLE_POSTERIZE:
    case GAMMA_TABLE_BACKLIGHT: {
      CDBG_ERROR("%s: Error Cannot apply contrast", __func__);
      return VFE_ERROR_INVALID_OPERATION;
    }
    default:;
  }
  mod->contrast = contrast;
  mod->update = TRUE;
  return status;
} /* vfe_gamma_set_contrast */

/*===========================================================================
 * FUNCTION    - vfe_gamma_init -
 *
 * DESCRIPTION:
 *==========================================================================*/
vfe_status_t vfe_gamma_init(int mod_id, void *mod_g, void *vparms)
{
  vfe_status_t status = VFE_SUCCESS;
  gamma_mod_t* mod = (gamma_mod_t*)mod_g;
  vfe_params_t *params = (vfe_params_t *)vparms;
  chromatix_parms_type *chromatix_ptr = params->chroma3a;

  CDBG("%s: enter", __func__);
  mod->trigger_update = FALSE;
  mod->contrast = DEFAULT_CONTRAST;
  mod->gamma_table_type = GAMMA_TABLE_DEFAULT;
  mod->cur_mode = VFE_OP_MODE_INVALID;
  mod->p_gamma_table[PREV] =
    chromatix_ptr->chromatix_rgb_default_gamma_table.gamma;
  mod->p_gamma_table[SNAP] =
    chromatix_ptr->chromatix_rgb_default_gamma_table_snapshot.gamma;
  mod->enable_backlight_compensation = TRUE;
  mod->trigger_enable = TRUE;
  //memcpy(mod->VFE_Y_GammaCfgCmd, VFE_DefaultY_GammaCfgCmd, sizeof(VFE_DefaultY_GammaCfgCmd));
#ifdef VFE_2X
  mod->VFE_Y_GammaCfgCmd = VFE_DefaultY_GammaCfgCmd;
#endif
  gamma_set_solarize_table(mod, chromatix_ptr);
  return status;
} /* vfe_gamma_init */


/*===========================================================================
 * FUNCTION    - vfe_gamma_reload_params -
 *
 * DESCRIPTION:
 *==========================================================================*/
vfe_status_t vfe_gamma_reload_params(int mod_id, void *mod_g, void *vparms)
{
  vfe_status_t status = VFE_SUCCESS;
  gamma_mod_t* mod = (gamma_mod_t*)mod_g;
  vfe_params_t *params = (vfe_params_t *)vparms;
  chromatix_parms_type *chromatix_ptr = params->chroma3a;
  mod->reload_params = TRUE;

  if (params->bs_mode != CAMERA_BESTSHOT_OFF) {
    CDBG("%s: update BSM in reload for mode %d",__func__,params->bs_mode);
    status= vfe_gamma_set_bestshot(mod_id, mod, params, params->bs_mode);
  }
  else {
    status= vfe_gamma_set_table(mod, params, GAMMA_TABLE_DEFAULT);
  }
  #ifdef VFE_2X
    mod->update = TRUE;
  #endif
  return status;
} /*vfe_gamma_reload_params*/

/*===========================================================================
 * FUNCTION    - vfe_gamma_trigger_enable -
 *
 * DESCRIPTION:
 *==========================================================================*/
vfe_status_t vfe_gamma_trigger_enable(int mod_id, void *mod_g, void *vparms,
  int enable)
{
  CDBG("%s: %d", __func__, enable);
  gamma_mod_t* mod = (gamma_mod_t*)mod_g;
  mod->trigger_enable = enable;
  return VFE_SUCCESS;
} /*vfe_gamma_trigger_enable*/

/*===========================================================================
 * FUNCTION    - vfe_gamma_tv_validate -
 *
 * DESCRIPTION:
 *==========================================================================*/
vfe_status_t vfe_gamma_tv_validate(int mod_id, void *test_input,
  void *test_output)
{
  int i = 0;
  uint32_t in, out;
  uint32_t in_delta, out_delta;
  uint32_t in_val, out_val;
  vfe_test_module_input_t* input = (vfe_test_module_input_t *)test_input;
  vfe_test_module_output_t* output = (vfe_test_module_output_t *)test_output;
  uint32_t mask = input->gamma.mask;

  for (i=0; i<VFE_GAMMA_NUM_ENTRIES; i++) {
    in = input->gamma.table[i] & mask;
    out = output->gamma_table[i] & mask;
    in_delta = in>>8;
    out_delta = out>>8;
    in_val = in & 0xFF;
    out_val = out & 0xFF;
    if (!MATCH(in_delta, out_delta, 0))
      CDBG_TV("%s: gamma[%d] in_delta %d out_delta %d doesnt match", __func__,
        i, in_delta, out_delta);
    if (!MATCH(in_val, out_val, 0))
      CDBG_TV("%s: gamma[%d] in_val %d out_val %d doesnt match", __func__,
        i, in_val, out_val);
  }
  return VFE_SUCCESS;
}/*vfe_gamma_tv_validate*/

/*===========================================================================
 * FUNCTION    - vfe_gamma_deinit -
 *
 * DESCRIPTION:
 *==========================================================================*/
vfe_status_t vfe_gamma_deinit(int mod_id, void *mod_g, void *vparms)
{
  return VFE_SUCCESS;
}
#ifdef VFE_32
/*===========================================================================
 * FUNCTION    - vfe_gamma_plugin_update -
 *
 * DESCRIPTION:
 *==========================================================================*/
vfe_status_t vfe_gamma_plugin_update(int module_id, void *mod,
  void *vparams)
{
  vfe_status_t status;
  gamma_module_t *cmd = (gamma_module_t *)mod;
  vfe_params_t *vfe_params = (vfe_params_t *)vparams;

  if (VFE_SUCCESS != vfe_util_write_hw_cmd(vfe_params->camfd,
     CMD_GENERAL, (void *)&(cmd->GammaCfgCmd),
     sizeof(VFE_GammaConfigCmdType),
     VFE_CMD_FRAME_SKIP_CFG)) {
     CDBG_HIGH("%s: Failed for op mode = %d failed\n", __func__,
       vfe_params->vfe_op_mode);
       return VFE_ERROR_GENERAL;
  }
  return VFE_SUCCESS;
} /* vfe_gamma_plugin_update */
#endif
